{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:48:55.244173Z",
     "start_time": "2018-05-11T04:48:54.403172Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from keras.applications.mobilenet import MobileNet\n",
    "from keras.preprocessing import image\n",
    "from keras.applications.mobilenet import preprocess_input\n",
    "import cv2\n",
    "from keras.models import load_model\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:48:56.331450Z",
     "start_time": "2018-05-11T04:48:55.257878Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector = load_model('smoke_detecctor_with_mobilenet_48.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:49:00.386280Z",
     "start_time": "2018-05-11T04:48:57.388473Z"
    }
   },
   "outputs": [],
   "source": [
    "model_mobilenet = MobileNet(input_shape=(224, 224, 3), include_top=False, weights='imagenet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:49:47.716821Z",
     "start_time": "2018-05-11T04:49:09.614839Z"
    }
   },
   "outputs": [],
   "source": [
    "cap = cv2.VideoCapture(0)\n",
    "#video_writer = cv2.VideoWriter('smoke_detection_output1.avi', cv2.VideoWriter_fourcc(*'MJPG'), 30, (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))\n",
    "\n",
    "steps = [5, 150, 500]\n",
    "#frames = [0] * 30\n",
    "choice = 0\n",
    "total_time = 0\n",
    "count = 0\n",
    "#num = 1\n",
    "while True:\n",
    "    status, img = cap.read()\n",
    "    #img = img[:, ::-1, :]\n",
    "    if status:\n",
    "        #img = img[100: 500, 200: 600, :]\n",
    "        '''img_detect = cv2.resize(img[:, :, [2, 1, 0]], (48 * num, 48 * num))\n",
    "        detection_results = []\n",
    "        for i in range(num):\n",
    "            for j in range(num):\n",
    "                tmp = img_detect[i * 48: (i + 1) * 48, j * 48: (j + 1) * 48, :]\n",
    "                tmp = cv2.resize(tmp, (224, 224))\n",
    "                start_time = time.time()\n",
    "                x = image.img_to_array(tmp)\n",
    "                x = np.expand_dims(x, axis=0)\n",
    "                x = preprocess_input(x)\n",
    "                feature = model_mobilenet.predict(x).reshape((1, 7*7*1024))\n",
    "                #print(model_smoke_detector.predict(feature)[0, 0])\n",
    "                result = model_smoke_detector.predict(feature)[0, 0]\n",
    "                detection_results.append(result)\n",
    "                end_time = time.time()\n",
    "\n",
    "                total_time += end_time - start_time\n",
    "                count += 1'''\n",
    "        \n",
    "        img_detect = cv2.resize(img[:, :, [2, 1, 0]], (224, 224))\n",
    "        #tmp = img_detect[i * 48: (i + 1) * 48, j * 48: (j + 1) * 48, :]\n",
    "        #tmp = cv2.resize(tmp, (224, 224))\n",
    "        start_time = time.time()\n",
    "        x = image.img_to_array(img_detect)\n",
    "        x = np.expand_dims(x, axis=0)\n",
    "        x = preprocess_input(x)\n",
    "        feature = model_mobilenet.predict(x).reshape((1, 7*7*1024))\n",
    "        #print(model_smoke_detector.predict(feature)[0, 0])\n",
    "        result = model_smoke_detector.predict(feature)[0, 0]\n",
    "        #detection_results.append(result)\n",
    "        end_time = time.time()\n",
    "\n",
    "        total_time += end_time - start_time\n",
    "        count += 1\n",
    "                \n",
    "        #frames.pop(0)\n",
    "        #frames.append(1 if result > 0.5 else 0)\n",
    "        #img = cv2.flip(img, 1)\n",
    "        cv2.putText(img,\n",
    "                    'Probability: {0}'.format(str(result)),\n",
    "                    (30, 30), \n",
    "                    cv2.FONT_HERSHEY_COMPLEX, \n",
    "                    1, \n",
    "                    (0, 0, 255), \n",
    "                    1)\n",
    "        cv2.putText(img,\n",
    "                    '{0}'.format('smoke' if result > 0.5 else 'no smoke'),\n",
    "                    (30, 90), \n",
    "                    cv2.FONT_HERSHEY_COMPLEX, \n",
    "                    2, \n",
    "                    (0, 0, 255), \n",
    "                    2)\n",
    "        '''cv2.putText(img,\n",
    "                    '{0}'.format('smoke' if sum(frames) > 0 else 'no smoke'),\n",
    "                    (30, 90), \n",
    "                    cv2.FONT_HERSHEY_COMPLEX, \n",
    "                    2, \n",
    "                    (0, 0, 255), \n",
    "                    2)'''\n",
    "        cv2.imshow('', img)\n",
    "        key = cv2.waitKey(steps[choice])\n",
    "        #video_writer.write(img)\n",
    "        if key == 27:\n",
    "            break\n",
    "        elif key == ord('n'):\n",
    "            choice += 1\n",
    "            choice %= 3\n",
    "    else:\n",
    "        cv2.destroyAllWindows()\n",
    "        break\n",
    "cap.release()\n",
    "#video_writer.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T04:49:50.444269Z",
     "start_time": "2018-05-11T04:49:50.432700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.020725930829447105"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_time / count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
